import YouXin from 'youxin-js-sdk';
import axios from '../api/request';
/**
 * 监听触摸滑动
 */
export function handleBanSwiper (vue, node) {
  if (node) {
    vue.$refs[node].$el.addEventListener('touchstart', (event) => {
      setJSBrageSwiper();
    });
  }
}

/**
 * 禁止原生滑动: 使用场景 在轮播图中滑动轮播图会受原生影响
 */
function setJSBrageSwiper () {
  try {
    window.UxinJsCore &&
      window.UxinJsCore.postMessage('{"method": "setViewPagerLrScroll", "params": { scrollAble: false }}');
  } catch (e) {
    console.log(`JSBrageHeight error: ${e}`);
  }
}
// 新开页面
export function openNewWindow (vue, path, query, showHeader = '0', title = '') {
  if (window.UxinJsCore) {
    let sql = '';
    if (query) {
      for (const key in query) {
        if (sql.length) {
          sql += `&${key}=${query[key]}`;
        } else {
          sql = `${key}=${query[key]}`;
        }
      }
    }
    const useUrl = window.location.origin,
      newUrl = `${useUrl}/nrmsui/resourceapl/mobile${path}?${sql}`;
    // newUrl = `${useUrl}${path}?${sql}`;//本机测试使用地址
    YouXin.getWindowId({
      success (WindowId) {
        // 成功方法
        console.log('地址', `${newUrl}&WindowId=${WindowId}`);
        YouXin.openWindow({
          url: `${newUrl}&WindowId=${WindowId}`, // 网页地址
          title: title,
          showHeader: showHeader // 是否显示导航：0 隐藏 1 显示（ 默认）
        });
      }
    });
  } else {
    const routeUrl = vue.$router.resolve({ path: path, query: query });
    window.open(routeUrl.href, '_blank');
    // vue.$router.push({ path: path, query: query });
  }
}
/**
 * @Desc 判断是否在 原生客户端环境
 */
export function isJudgeEnv (fn) {
  window.UxinJsCore ? fn() : console.error('请在客户端中使用');
}

// 获取资源观看人数
export async function getRecordCountStat ({ catId = 'enter_nrms', purpose = 4, appId = 'enter_nrms', resId }) {
  const { data } = await axios.get(`${process.env.VUE_APP_NRMS_URL}/rms/v1/cat_records/get_record_count_stat`, {
    params: { catId, purpose, appId, resId }
  });
  return data;
}
export function setRecordCountStat (resId) {
  return new Promise((resolve, reject) => {
    getRecordCountStat({ resId })
      .then(({ data }) => {
        if (data) {
          const res = JSON.parse(JSON.stringify(data || []));
          resolve(res);
        } else {
          reject(new Error('请求错误'));
        }
      })
      .catch((e) => {
        reject(e);
      });
  });
}

// 获取资源价格
export async function getProdSuitesByGoods ({ goodsCategoryId, goodsLabel, suiteId, clientType }) {
  const { data } = await axios.get(`${process.env.VUE_APP_UC_URL}/pbs/prodsuite/getProdSuitesByGoods`, {
    params: { goodsCategoryId, goodsLabel, suiteId, clientType }
  });
  return data;
}
export function setProdSuitesByGoods (goodsCategoryId, goodsLabel, suiteId, clientType) {
  return new Promise((resolve, reject) => {
    getProdSuitesByGoods({ goodsCategoryId, goodsLabel, suiteId, clientType })
      .then(({ data }) => {
        if (data !== undefined) {
          const res = JSON.parse(JSON.stringify(data || []));
          resolve(res);
        } else {
          reject(new Error('请求错误'));
        }
      })
      .catch((e) => {
        reject(e);
      });
  });
}
